package com.itcpeng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcpeng.constant.MessageConstant;
import com.itcpeng.dao.MemberDao;
import com.itcpeng.dao.OrderDao;
import com.itcpeng.dao.OrderSettingDao;
import com.itcpeng.exception.MyException;
import com.itcpeng.pojo.Member;
import com.itcpeng.pojo.Order;
import com.itcpeng.pojo.OrderSetting;
import com.itcpeng.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
public class OrderServiceImol implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;


    //预约体检
    @Override
    public Order submitOrder(Map<String, String> map) throws MyException {
        //判断是否可预约
        String orderdate = map.get("orderDate");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date data = null;
        try {
            data = sdf.parse(orderdate);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException("日期转换失败:data" + (data == null ? "" : data));
        }
        OrderSetting os = orderSettingDao.findByOrderDate(data);
        if (os == null) {
            throw new MyException(MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        if (os.getNumber() <= os.getReservations()) {
            throw new MyException(MessageConstant.ORDER_FULL);
        }
        String telephone = map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        //可预约 判断是否为会员
        if (member == null) {
            //不是会员 注册为会员 取出id  将添加订单表时可以用
            member = new Member();
            member.setRegTime(new Date());
            member.setPhoneNumber(telephone);
            member.setSex(map.get("sex"));
            member.setName(map.get("name"));
            member.setIdCard(map.get("idCard"));
            memberDao.add(member);
        }
        //是会员  取出id 将添加订单表时可以用
        Integer memberId = member.getId();

        //判断是否预约过了
        Order order = new Order();
        order.setMemberId(memberId);
        order.setPackageId(Integer.valueOf(map.get("packageId")));
        order.setOrderDate(data);
        List<Order> orders = orderDao.findByCondition(order);
        if (orders != null && orders.size() > 0) {
            throw new MyException(MessageConstant.HAS_ORDERED);
        }
        //没预约则预约成功
        order.setOrderStatus("未到诊");
        order.setOrderType(map.get("orderType"));
        orderDao.add(order);
        //跟新已预约人数
        orderSettingDao.editReservationsByOrderDate(os);
        return order;
    }


    //查询预约信息
    @Override
    public Map<String, String> findById(Integer id) {
        return orderDao.findById4Detail(id);
    }

}
